package cn.time1024.time_app.ui.music

import android.media.AudioAttributes
import android.media.MediaPlayer
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import cn.time1024.time_app.databinding.FragmentMusicBinding

class MusicFragment : Fragment() {

    private var _binding: FragmentMusicBinding? = null
    private val binding get() = _binding!!

    private val tracks = listOf(
        Pair("白噪音·雨声", "https://www2.cs.uic.edu/~i101/SoundFiles/StarWars60.wav"),
        Pair("白噪音·咖啡馆", "https://www2.cs.uic.edu/~i101/SoundFiles/PinkPanther30.wav")
    )
    private var player: MediaPlayer? = null
    private var currentUrl: String? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = FragmentMusicBinding.inflate(inflater, container, false)
        setupList()
        setupControls()
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView(); release()
        _binding = null
    }

    private fun setupList() {
        binding.rvTracks.layoutManager = LinearLayoutManager(requireContext())
        binding.rvTracks.adapter = object : androidx.recyclerview.widget.RecyclerView.Adapter<TrackVH>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrackVH {
                val v = layoutInflater.inflate(cn.time1024.time_app.R.layout.item_track, parent, false)
                return TrackVH(v)
            }
            override fun getItemCount(): Int = tracks.size
            override fun onBindViewHolder(holder: TrackVH, position: Int) {
                val (title, url) = tracks[position]
                holder.tvTitle.text = title
                holder.btnPlay.setOnClickListener { play(url); binding.tvNow.text = title }
            }
        }
    }

    private fun setupControls() {
        binding.btnPause.setOnClickListener { player?.pause() }
        binding.btnResume.setOnClickListener { player?.start() }
        binding.btnStop.setOnClickListener { release(); binding.tvNow.text = "未播放" }
    }

    private fun play(url: String) {
        release()
        currentUrl = url
        player = MediaPlayer().apply {
            setAudioAttributes(
                AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .build()
            )
            setDataSource(url)
            setOnPreparedListener { start() }
            prepareAsync()
        }
    }

    private fun release() { player?.release(); player = null; currentUrl = null }
}

class TrackVH(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
    val tvTitle: android.widget.TextView = itemView.findViewById(cn.time1024.time_app.R.id.tvTitle)
    val btnPlay: View = itemView.findViewById(cn.time1024.time_app.R.id.btnPlay)
}

